package cn.stylefeng.guns.modular.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.modular.business.dto.DeviceConfigQuery;
import cn.stylefeng.guns.modular.business.dto.DeviceConfigUpdateOrAddDto;
import cn.stylefeng.guns.modular.business.entity.Device;
import cn.stylefeng.guns.modular.business.entity.DeviceBrand;
import cn.stylefeng.guns.modular.business.entity.Project;
import cn.stylefeng.guns.modular.business.entity.ProjectDevice;
import cn.stylefeng.guns.modular.business.exception.DeviceBrandExceptionEnum;
import cn.stylefeng.guns.modular.business.exception.DeviceConfigExceptionEnum;
import cn.stylefeng.guns.modular.business.exception.ProjectExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.DeviceBrandMapper;
import cn.stylefeng.guns.modular.business.mapper.ProjectDeviceMapper;
import cn.stylefeng.guns.modular.business.mapper.ProjectMapper;
import cn.stylefeng.guns.modular.business.service.DeviceConfigService;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.dsctn.api.annotation.DataSource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DeviceConfigServiceImpl implements DeviceConfigService {

    @Autowired
    private ProjectDeviceMapper projectDeviceMapper;

    @Autowired
    private DeviceBrandMapper deviceBrandMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    @DataSource(name = "business")
    public PageResult<ProjectDevice> findPage(DeviceConfigQuery deviceConfigQuery) {
        Page<ProjectDevice>  page = new Page<>(deviceConfigQuery.getPageNo(),deviceConfigQuery.getPageSize());
        QueryWrapper<ProjectDevice> qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(deviceConfigQuery.getDeviceSn())) {
            deviceConfigQuery.setProjectId(deviceConfigQuery.getDeviceSn().toUpperCase().trim());
        }
        qw.like(StringUtils.isNotBlank(deviceConfigQuery.getDeviceSn()), ProjectDevice.COL_DEVICE_SN,deviceConfigQuery.getDeviceSn());
        qw.like(StringUtils.isNotBlank(deviceConfigQuery.getProjectName()), ProjectDevice.COL_PROJECT_NAME,deviceConfigQuery.getProjectName());
        qw.eq(deviceConfigQuery.getStatus()!=null, Device.COL_STATUS,deviceConfigQuery.getStatus());
        qw.orderByDesc(Device.COL_ID);
        this.projectDeviceMapper.selectPage(page,qw);
        return PageResultFactory.createPageResult(page);
    }

    @Override
    @DataSource(name = "business")
    public ProjectDevice detail(Integer id) {
        ProjectDevice projectDevice = this.projectDeviceMapper.selectById(id);
        if(ObjectUtil.isEmpty(projectDevice)) {
            throw new BusinessException(DeviceConfigExceptionEnum.DEVICECONFIG_NOT_EXISTED);
        }
        return projectDevice;
    }

    @Override
    @DataSource(name = "business")
    public void editOrAdd(DeviceConfigUpdateOrAddDto deviceConfigUpdateDto) {




        // 品牌是否存在
        QueryWrapper<DeviceBrand> qwBrand = new QueryWrapper<>();
        qwBrand.eq(DeviceBrand.COL_DEVCIE_BRAND_ID,deviceConfigUpdateDto.getDeviceBrandId());
        DeviceBrand deviceBrand = this.deviceBrandMapper.selectOne(qwBrand);
        if(ObjectUtil.isEmpty(deviceBrand)) {
            throw new BusinessException(DeviceBrandExceptionEnum.DEVICEBRAND_NOT_EXISTED);
        }

        // 项目是否存在
        QueryWrapper<Project> qwProject = new QueryWrapper<>();
        qwProject.eq(Project.COL_PROJECT_ID,deviceConfigUpdateDto.getProjectId());
        Project project = this.projectMapper.selectOne(qwProject);
        if(ObjectUtil.isEmpty(project)) {
            throw new BusinessException(ProjectExceptionEnum.PROJECT_NOT_EXISTED);
        }

        // 项目配置是否已经存在
        QueryWrapper<ProjectDevice> qwProjectDevice = new QueryWrapper<>();
        qwProjectDevice.eq(ProjectDevice.COL_PROJECT_ID,deviceConfigUpdateDto.getProjectId());
        qwProjectDevice.eq(ProjectDevice.COL_DEVICE_BRAND_ID,deviceConfigUpdateDto.getDeviceBrandId());
        qwProjectDevice.eq(ProjectDevice.COL_DEVICE_TYPE,deviceConfigUpdateDto.getDeviceType());
        qwProjectDevice.eq(ProjectDevice.COL_DEVICE_SN,deviceConfigUpdateDto.getDeviceSn());
        ProjectDevice projectConfig = this.projectDeviceMapper.selectOne(qwProjectDevice);
        if(!ObjectUtil.isEmpty(projectConfig) && null == deviceConfigUpdateDto.getId()) {
            throw new BusinessException(DeviceConfigExceptionEnum.DEVICECONFIG_EXISTED);
        }else if(!ObjectUtil.isEmpty(projectConfig) && null!=deviceConfigUpdateDto.getId() && !deviceConfigUpdateDto.getId().equals(projectConfig.getId())){
            // 修改查出来不是本身
            throw new BusinessException(DeviceConfigExceptionEnum.DEVICECONFIG_EXISTED);
        }


        ProjectDevice projectDevice = null;
        if(null == deviceConfigUpdateDto.getId()) {
            projectDevice = new ProjectDevice();
            long deviceEnterTime = 0;
            if(null != deviceConfigUpdateDto.getDeviceEntryTime()) {
                 deviceEnterTime = deviceConfigUpdateDto.getDeviceEntryTime().getTime() / 1000;
            }
            BeanUtil.copyProperties(deviceConfigUpdateDto,projectDevice,"deviceEntryTime","createTime","lastTime");
            projectDevice.setDeviceBrand(deviceBrand.getName());
            projectDevice.setProjectName(project.getProjectName());
            projectDevice.setDeviceEntryTime((int)deviceEnterTime);
            projectDevice.setCreateTime((int)(new Date().getTime() / 1000));
            projectDevice.setLastTime(DateUtil.date());
            projectDevice.setInstallTime(0);
            this.projectDeviceMapper.insert(projectDevice);
        }else { //更新

            // 设备配置是否存在
            projectDevice = this.projectDeviceMapper.selectById(deviceConfigUpdateDto.getId());
            if(ObjectUtil.isEmpty(projectDevice)) {
                throw new BusinessException(DeviceConfigExceptionEnum.DEVICECONFIG_NOT_EXISTED);
            }

            long deviceEnterTime = deviceConfigUpdateDto.getDeviceEntryTime().getTime() / 1000;
            long deviceCreateTime = deviceConfigUpdateDto.getCreateTime().getTime() / 1000;
            BeanUtil.copyProperties(deviceConfigUpdateDto,projectDevice,"deviceEntryTime","createTime","lastTime");
            projectDevice.setDeviceBrand(deviceBrand.getName());
            projectDevice.setProjectName(project.getProjectName());
            projectDevice.setDeviceEntryTime((int)deviceEnterTime);
            projectDevice.setCreateTime((int)deviceCreateTime);
            projectDevice.setLastTime(deviceConfigUpdateDto.getLastTime());
            this.projectDeviceMapper.updateById(projectDevice);
        }


    }


}
